//
//  SMBPickerView.m
//  SMBPickerView
//
//  Created by Zongzhu on 16/4/11.
//  Copyright © 2016年 Zongzhu. All rights reserved.
//

#import "SMBPickerView.h"


@interface SMBPickerView()
@property (nonatomic, strong) UIView *pickerViewContainerView;
@property (nonatomic, strong) UIView *pickerContainerView;
@property (nonatomic, strong) UIView *pickerTopBarView;
@property (nonatomic, strong) UIToolbar *pickerViewToolBar;
@end

@implementation SMBPickerView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:[self.class rootView].bounds];
    if (self) {
        [self initialize];
    }
    return self;
}
- (void)initialize{
    CGFloat pickerViewWidth = self.bounds.size.width;
    
    [self setBackgroundColor:[UIColor clearColor]];
    
    _pickerViewContainerView = [[UIView alloc] initWithFrame:self.bounds];
    _pickerContainerView.userInteractionEnabled = YES;
    [_pickerViewContainerView setBackgroundColor: [UIColor colorWithRed:0.412 green:0.412 blue:0.412 alpha:0.7]];
    [self addSubview:_pickerViewContainerView];
    

    _pickerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, _pickerViewContainerView.bounds.size.height - 260.0, _pickerViewContainerView.bounds.size.width, 260.0)];
    
    [_pickerViewContainerView addSubview:_pickerContainerView];
    
    
    //Top bar view
    _pickerTopBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, _pickerContainerView.frame.size.width, 44.0)];
    [_pickerContainerView addSubview:_pickerTopBarView];
    [_pickerTopBarView setBackgroundColor:[UIColor whiteColor]];
    
    
    _pickerViewToolBar = [[UIToolbar alloc] initWithFrame:_pickerTopBarView.frame];
    [_pickerContainerView addSubview:_pickerViewToolBar];
    
    
    [_pickerViewToolBar setBackgroundColor:_toolbarBackgroundColor];
    _pickerViewToolBar.barTintColor = _toolbarBackgroundColor;
    
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    _cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 44)];
    _cancelButton.backgroundColor = [UIColor clearColor];
    [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelButton setTitleColor:ThemeColor forState:UIControlStateNormal];
    [_cancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *cancelBarbutton= [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    UIBarButtonItem *cancelBarbutton = [[UIBarButtonItem alloc]initWithCustomView:_cancelButton];
    
    _completeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 44)];
    _completeButton.backgroundColor = [UIColor clearColor];
    [_completeButton setTitle:@"确定" forState:UIControlStateNormal];
    [_completeButton setTitleColor:ThemeColor forState:UIControlStateNormal];
    [_completeButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    
//    UIBarButtonItem *doneBarbutton = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
    UIBarButtonItem *doneBarbutton = [[UIBarButtonItem alloc]initWithCustomView:_completeButton];
    
    _pickerViewToolBar.items = @[cancelBarbutton,flexibleSpace, doneBarbutton];
    [cancelBarbutton setTintColor:_toolbarButtonTextColor];
    [doneBarbutton setTintColor:_toolbarButtonTextColor];
    
    [_pickerContainerView addSubview:[self pickerViewWithRect:CGRectMake(0.0, 44.0, pickerViewWidth, 216.0)]];
    
    // hidden
    [_pickerContainerView setTransform:CGAffineTransformMakeTranslation(0.0, CGRectGetHeight(_pickerContainerView.frame))];
    
    // selected row
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self initializePickerView];
    [self.pickerView setNeedsLayout];
    [self.pickerView layoutIfNeeded];
}
#pragma mark - Dismiss Methods

- (void)cancel{
    if (self.onDismissCancel) {
        [self setPickerHidden:YES callBack:self.onDismissCancel];
    }else{
        [self setPickerHidden:YES callBack:nil];
    }
}
-(void)dismiss{
    [self setPickerHidden:YES callBack:self.onDismissCompletion];
}
-(void)dealloc{
    NSLog(@"picker view %s dealloc",__func__);
}
#pragma mark - Show/hide PickerView methods

-(void)setPickerHidden: (BOOL)hidden
              callBack: (void(^)(NSString *))callBack{
    
    [UIView animateWithDuration:0.3
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         if (hidden) {
                             [_pickerViewContainerView setAlpha:0.0];
                             [_pickerContainerView setTransform:CGAffineTransformMakeTranslation(0.0, CGRectGetHeight(_pickerContainerView.frame))];
                         } else {
                             [_pickerViewContainerView setAlpha:1.0];
                             [_pickerContainerView setTransform:CGAffineTransformIdentity];
                         }
                     } completion:^(BOOL completed) {
                         if(completed && hidden){
                             [self removeFromSuperview];
                             if (callBack) {
                                 callBack([self selectedObject]);
                             }
                         }
                     }];
    
}
-(id)pickerViewWithRect:(CGRect)rect{
    @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)] userInfo:nil];
    return nil;
}
- (void)initializePickerView{

}
+(UIView*)rootView
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }
    return topController.view;
    
}
- (void)donePicker:(BOOL)hidden selectString:(NSString *)selectString callBack:(void (^)(NSString *))callBack{}
@end
